package com.vehicle.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vehicle.bean.DataGridView;
import com.vehicle.bean.PageVo;
import com.vehicle.mapper.OutwardRecordMapper;
import com.vehicle.pojo.OutwardRecord;
import com.vehicle.service.OutwardRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class OutwardRecordServiceImpl implements OutwardRecordService {

    @Autowired
    private OutwardRecordMapper outwardRecordMapper;

    //寻找所有出行记录
    @Override
    public DataGridView findAllOutwardRecord(String startDate, String endDate, PageVo pageVo) {
        PageHelper.startPage(pageVo.getPage(),pageVo.getLimit());
        DataGridView dataGridView = new DataGridView();
        List<OutwardRecord> allOutwardRecord = outwardRecordMapper.findAllOutwardRecord(startDate, endDate);
        PageInfo pageInfo = new PageInfo(allOutwardRecord);
        dataGridView.setCount(pageInfo.getTotal());
        dataGridView.setData(allOutwardRecord);
        return dataGridView;
    }

    @Override
    public DataGridView deleteOutward(Integer outwardid) {
        DataGridView dataGridView = new DataGridView();
        try{
            outwardRecordMapper.deleteOutward(outwardid);
            dataGridView.setCode(1001);
            dataGridView.setMsg("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            dataGridView.setCode(-1001);
            dataGridView.setMsg("删除失败");
        }
        return dataGridView;
    }

    @Override
    public OutwardRecord findOutwardRecordById(Integer outwardid) {
        OutwardRecord outwardRecord = outwardRecordMapper.findOutwardById(outwardid);
        return outwardRecord;
    }
}
